package com.hss.base;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.widget.Toast;

import com.afollestad.materialdialogs.MaterialDialog;
import com.hss.base.events.EventFinshActivity;
import com.hss.base.interfaces.IDestory;
import com.hss.base.util.ActivityUtils;
import com.hss.base.util.StartActivityUtil;
import com.hss.base.util.StatusBarUtils;
import com.jaeger.library.StatusBarUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Created by Administrator on 2017/2/9.
 */

public abstract class BaseActivty extends AppCompatActivity implements IDestory {

    MaterialDialog lodingDilog = null;//loading dialog

    public void initIntentData() {

    }

    public void initData() {

    }

    public void initView() {

    }

    public void initListener() {
    }

    public void initSystemBarTint(int col) {
        // TODO Auto-generated method stub
        StatusBarUtil.setColorNoTranslucent(this, col);
    }


    public CompositeSubscription mCompositeSubscription;

    //RXjava取消注册，以避免内存泄露
    public void onUnsubscribe() {
        if (mCompositeSubscription != null && mCompositeSubscription.hasSubscriptions()) {
            mCompositeSubscription.unsubscribe();
        }

    }

    public void addSubscription(Observable observable, Subscriber subscriber) {
        if (mCompositeSubscription == null) {
            mCompositeSubscription = new CompositeSubscription();
        }
        mCompositeSubscription.add(observable
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber));
    }


    public void clearSubscription() {
        if (mCompositeSubscription != null) {
            mCompositeSubscription.clear();
        }
    }


   /* protected Toolbar initToolBar(String title) {

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        if (toolbar != null) {
            setSupportActionBar(toolbar);
//            TextView toolbaTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
//            toolbaTitle.setText(title);
        }
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setDisplayShowTitleEnabled(false);
        }
        return toolbar;
    }

     Toolbar initToolBarAsHome(String title) {

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        if (toolbar != null) {
            setSupportActionBar(toolbar);
//            TextView toolbaTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
//            toolbaTitle.setText(title);
        }
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(false);
            actionBar.setDisplayShowTitleEnabled(false);
        }
        return toolbar;
    }*/


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {

            case android.R.id.home:
                super.onBackPressed();//返回
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }

    }

    @Override
    public boolean onSupportNavigateUp() {
        onBackPressed();
        return true;
    }


    @Override
    public void destoryRes() {
        clearSubscription();
        hideLoadingDialog();
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        if (getActionBar() != null) {
            getActionBar().hide();
        }

        if (getActionBar() != null) {
            getActionBar()  .setDisplayHomeAsUpEnabled(false);
            getActionBar().hide();
        }

        StatusBarUtils.setStatusBarLightMode(getWindow());
    }

    @Override
    protected void onDestroy() {
        onUnsubscribe();

        destoryRes();
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onStop() {
        super.onStop();

    }

    public void exitFunction() {
        exit();
    }

    public void exit() {
        finish();
    }

    public void showToast(String text) {
        Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
    }

    public void showToast(int textId) {
        Toast.makeText(getApplicationContext(), textId, Toast.LENGTH_SHORT).show();
    }

    /**
     * hss
     * 2017 2  11
     *
     * @param cls     fragment class
     * @param frameId frameLayout id
     * @return fragment
     */
    public <T extends Fragment> T addFragment2Frame(Class<T> cls, int frameId) throws IllegalAccessException, InstantiationException {
        T fragment = null;
        fragment = (T) getSupportFragmentManager().findFragmentById(frameId);
        if (null == fragment) {
            fragment = cls.newInstance();
            ActivityUtils.addFragmentToActivity(getSupportFragmentManager(),
                    fragment, frameId);

        }
        return fragment;
    }


    @Override
    protected void onResumeFragments() {
        super.onResumeFragments();
    }

    public void showLoadingDialog() {
        if (isFinishing()) {
            return;
        }
        lodingDilog = new MaterialDialog.Builder(this)
                .content(R.string.loading)
                .progress(true, 0)
                .show();
    }

    public void hideLoadingDialog() {
        if (lodingDilog != null) {
            lodingDilog.dismiss();
        }
    }

    public <T extends AppCompatActivity> void startActy(Class<T> clazz) {
        StartActivityUtil.startActivity(clazz, this);
    }

    public <T extends AppCompatActivity> void startActy(Class<T> clazz, Bundle data) {
        StartActivityUtil.startActivity(clazz, data, this);

    }

    public <T extends AppCompatActivity> void startActyForResult(Class<T> clazz, Bundle data, int requestCode) {
        StartActivityUtil.startActivityForResult(clazz, requestCode, data, this);

    }

    public <T extends AppCompatActivity> void startActyForResult(Class<T> clazz, int requestCode) {
        StartActivityUtil.startActivityForResult(clazz, requestCode, this);

    }


    public void finishActivity() {
        exitFunction();
    }

    @Override
    public void finishActivity(int resultCode) {
        setResult(resultCode);
        exitFunction();
    }

    public void finishActivity(int resultCode, Intent data) {
        setResult(resultCode, data);
        exitFunction();
    }


    //    @Subscribe(priority = 1);
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(EventFinshActivity event) {
        if (!event.aliveActivityName.equals(this.getClass().getSimpleName())) {
            finishActivity();
        }
    }

}
